package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.AppliDetail;
import com.ruoyi.system.domain.AppliDetailMat;
import com.ruoyi.system.domain.Requisitions;
import com.ruoyi.system.mapper.AppliDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ApplianceMapper;
import com.ruoyi.system.domain.Appliance;
import com.ruoyi.system.service.IApplianceService;

/**
 * 器具租赁Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-29
 */
@Service
public class ApplianceServiceImpl implements IApplianceService 
{
    @Autowired
    private ApplianceMapper applianceMapper;

    @Autowired
    private AppliDetailMapper appliDetailMapper;

    /**
     * 查询器具租赁列表
     * 
     * @param appliance 器具租赁
     * @return 器具租赁
     */
    @Override
    public List<Appliance> selectApplianceList(Appliance appliance)
    {
        return applianceMapper.selectApplianceList(appliance);
    }

    /**
     * 新增器具租赁单
     */
    @Override
    public int insertAppliance(Requisitions requisitions)
    {
        int result=applianceMapper.insertRequisitions(requisitions);
        if (null!=requisitions.getAppliances()){
            for (int i = 0; i < requisitions.getAppliances().size(); i++) {
                requisitions.getAppliances().get(i).setReqId(requisitions.getRid());
                applianceMapper.insertAppliance(requisitions.getAppliances().get(i));
            }
        }
        return result;
    }

    /**
     * 修改器具租赁
     */
    @Override
    public int updateAppliance(Requisitions requisitions)
    {
        int result=applianceMapper.updateRequisitions(requisitions);
        if (null!=requisitions.getAppliances()){
            //删除原有数据
            applianceMapper.deleteApplianceByrId(requisitions.getRid());
            for (int i = 0; i < requisitions.getAppliances().size(); i++) {
                //赋值新增
                requisitions.getAppliances().get(i).setReqId(requisitions.getRid());
                applianceMapper.insertAppliance(requisitions.getAppliances().get(i));
            }
        }
        return result;
    }

    @Override
    public List<Requisitions> queryApplianceList(Requisitions requisitions) {
        return applianceMapper.queryApplianceList(requisitions);
    }

    @Override
    public Requisitions queryApplianceListById(Long id) {
        return applianceMapper.queryApplianceListById(id);
    }

    @Override
    public int deleteRequisitionsById(Long ids) {
        applianceMapper.deleteApplianceByrId(ids);
        int result=applianceMapper.deleteRequisitionsById(ids);
        return result;
    }

    @Override
    public int insertPurchase(Requisitions requisitions) {
        //拿出请购单值赋值
        AppliDetail appliDetail = new AppliDetail();
        appliDetail.setProName(requisitions.getProName());
        appliDetail.setProNumber(requisitions.getProNumber());
        appliDetail.setDeliveryReq(requisitions.getDeliveryReq());
        appliDetail.setRequisitionDate(requisitions.getRequisitionDate());
        appliDetail.setReqId(requisitions.getRid());
        //新增采购单拿到新增出的ID
        int insertAppliDetail=appliDetailMapper.insertAppliDetail(appliDetail);
        //不为空进入赋值
        if (null!=requisitions.getAppliances()){
            for (int i = 0; i < requisitions.getAppliances().size(); i++) {
                AppliDetailMat appliDetailMat = new AppliDetailMat();
                appliDetailMat.setAppliDetId(appliDetail.getAppliId());
                appliDetailMat.setAppliName(requisitions.getAppliances().get(i).getAppliName());
                appliDetailMat.setAppliModle(requisitions.getAppliances().get(i).getAppliModle());
                appliDetailMat.setAppliSpecifications(requisitions.getAppliances().get(i).getAppliSpecifications());
                appliDetailMat.setAppliUnit(requisitions.getAppliances().get(i).getAppliUnit());
                appliDetailMat.setAppliSum(requisitions.getAppliances().get(i).getAppliSum());
                appliDetailMapper.insertAppliDetailMat(appliDetailMat);
            }
        }
        if (insertAppliDetail>=0){
            //修改状态已生成
            requisitions.setState(1);
            applianceMapper.updateRequisitions(requisitions);
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int deleteAppliDetailById(Long appliId) {
        //查询原有的请购单
        Requisitions requisitions=applianceMapper.selectAppliance(appliId);
        if (null!=requisitions){
            //修改状态

            requisitions.setState(0);
            applianceMapper.updateRequisitions(requisitions);
        }
        //删除两表
        appliDetailMapper.deleteAppliDetailMat(appliId);
        appliDetailMapper.deleteAppliDetailById(appliId);
        return 1;
    }
}
